using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.Cookies;
using DeathReportManagement.Data;
using DeathReportManagement.Services;

var builder = WebApplication.CreateBuilder(args);

// 添加服务
builder.Services.AddDbContext<DeathReportContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.LoginPath = "/Account/Login";
        options.LogoutPath = "/Account/Logout";
        options.AccessDeniedPath = "/Account/AccessDenied";
        options.ExpireTimeSpan = TimeSpan.FromHours(8);
        options.SlidingExpiration = true;
    });

builder.Services.AddAuthorization();

builder.Services.AddControllersWithViews();

builder.Services.AddHttpContextAccessor();

// 注册服务
builder.Services.AddScoped<AuthService>();
builder.Services.AddScoped<DeathReportService>();
builder.Services.AddScoped<OperationLogService>();
builder.Services.AddScoped<DictionaryService>();
builder.Services.AddScoped<HisIntegrationService>();
builder.Services.AddScoped<ReportSubmissionService>();
builder.Services.AddScoped<Icd10Service>();

builder.Services.AddHttpClient();

var app = builder.Build();

// 配置管道
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();